package pl.edu.agh.pp.uhd.scp;


import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.upnp.UPnPDevice;


public class DeviceListener implements ServiceListener{

	@Override
	public void serviceChanged(ServiceEvent event) {
	
		switch(event.getType()){
			case ServiceEvent.MODIFIED:{
				System.out.println("modif");;
				
			}break;
			case ServiceEvent.REGISTERED:{
				System.out.println("reg'ed");
				
			}break;
			case ServiceEvent.UNREGISTERING:{
				System.out.println("unreg'ed");;
			}break;
		}
		
	}

	public void discover(){
		
		System.out.println("Discovering UPnP devices");
		try {
			Activator.context.addServiceListener(this,
					"(&"+
						"("+Constants.OBJECTCLASS+"="+UPnPDevice.class.getName()+")"+
						"("+UPnPDevice.UDN+"=*)"+
					")"
					);
		} catch (InvalidSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ServiceReference[] roots = null; 
		try {
			roots = Activator.context.getServiceReferences(
					UPnPDevice.class.getName(),
					"(&"+
					"("+Constants.OBJECTCLASS+"="+UPnPDevice.class.getName()+")"+
					"("+UPnPDevice.UDN+"=*)"+
					")"
				);
		} catch (InvalidSyntaxException e) {
			e.printStackTrace();
		}
		
		for(ServiceReference sr:roots){
			System.out.println(sr.getProperty(UPnPDevice.UDN));
		}
		
	}
	
	public void close(){
		Activator.context.removeServiceListener(this);
		
	}
	
	

}
